package com.demo.designmode.singleton;

/**
 * @Author: jxm
 * @Description: 懒汉式 双重检查机制 线程安全 volatile（禁止指令重排）
 *  双重检查（Double check Lock 简称：DCL）
 * @Date: 2022/6/7 16:37
 * @Version: 1.0
 */
public class LazySingleton3 {

    private LazySingleton3(){}

    private volatile static LazySingleton3 singleton;

    public static LazySingleton3 getInstance(){
        if(singleton == null){
            synchronized (LazySingleton3.class){
                if(singleton == null){
                    singleton = new LazySingleton3();
                }
            }
        }
        return singleton;
    }

    public static void main(String[] args) {
        for (int i =0;i<10;i++){
            new Thread(()->{
                LazySingleton3 instance = LazySingleton3.getInstance();
                System.out.println(Thread.currentThread().getName()+"=="+instance.hashCode());
            }).start();
        }
    }
}
